@echo off
@echo Building NVM-DEV for Windows
@echo ==========================

SET INNOSETUP=%CD%\nvm-dev.iss
SET ORIG=%CD%
REM SET GOPATH=%CD%\src
SET GOBIN=%CD%\bin
REM Support for older architectures
SET GOARCH=386

@echo Checking for Go installation...
go version >nul 2>&1
if %errorlevel% neq 0 (
  @echo ERROR: Go is not installed or not in PATH.
  @echo Please install Go 1.21 or higher and make sure it's in your PATH.
  @echo Download from: https://golang.org/dl/
  pause
  exit /b 1
)

@echo Go found. Proceeding with build...

REM Cleanup existing build if it exists
@echo Cleaning up previous builds...
if exist src\nvm-dev.exe (
  del src\nvm-dev.exe
)
if exist %GOBIN%\nvm-dev.exe (
  del %GOBIN%\nvm-dev.exe
)

REM Make the executable and add to the binary directory
@echo Building nvm-dev.exe...
cd .\src
go build -o nvm-dev.exe nvm.go
if %errorlevel% neq 0 (
  @echo ERROR: Failed to build nvm-dev.exe
  cd ..
  pause
  exit /b 1
)

REM Group the file with the helper binaries
@echo Moving nvm-dev.exe to bin directory...
move nvm-dev.exe %GOBIN%
cd ..\

REM Skip codesigning for dev builds
@echo Skipping codesigning for development build...

@echo Getting version info...
for /f %%i in ('"%GOBIN%\nvm-dev.exe" version') do set AppVersion=%%i
echo nvm-dev.exe v%AppVersion% built.

REM Create the distribution folder
SET DIST=%CD%\dist-dev\%AppVersion%

REM Remove old build files if they exist.
if exist %DIST% (
  echo Clearing old build in %DIST%
  rd /s /q "%DIST%"
)

REM Create the distribution directory
mkdir "%DIST%"

REM Create the "no install" zip version
for %%a in ("%GOBIN%") do (buildtools\zip -j -9 -r "%DIST%\nvm-dev-noinstall.zip" "%CD%\LICENSE" %%a\* -x "%GOBIN%\nodejs.ico")

REM Generate update utility
echo Generating update utility...
cd .\updater
go build -o nvm-update-dev.exe nvm-update.go
if %errorlevel% neq 0 (
  @echo WARNING: Failed to build nvm-update-dev.exe
  cd ..
) else (
  echo Skipping codesigning for development build...
  move nvm-update-dev.exe %DIST%
)
cd ..\

REM Generate the installer (InnoSetup)
echo Generating installer...
if exist "%INNOSETUP%" (
  buildtools\iscc "%INNOSETUP%" "/o%DIST%"
  if %errorlevel% neq 0 (
    @echo WARNING: Failed to generate installer
  ) else (
    echo Skipping codesigning for development build...
    echo Bundle the installer/updater...
    if exist "%DIST%\nvm-dev-setup.exe" (
      buildtools\zip -j -9 -r "%DIST%\nvm-dev-setup.zip" "%DIST%\nvm-dev-setup.exe"
    )
    if exist "%DIST%\nvm-update-dev.exe" (
      buildtools\zip -j -9 -r "%DIST%\nvm-dev-update.zip" "%DIST%\nvm-update-dev.exe"
    )
    
    if exist "%DIST%\nvm-update-dev.exe" del "%DIST%\nvm-update-dev.exe"
    if exist "%DIST%\nvm-dev-setup.exe" del "%DIST%\nvm-dev-setup.exe"
  )
) else (
  @echo WARNING: Inno Setup script not found: %INNOSETUP%
)

REM Generate checksums
if exist "%DIST%" (
  echo Generating checksums...
  for %%f in (%DIST%\*.*) do (
    if not "%%~xf"==".checksum.txt" (
      certutil -hashfile "%%f" MD5 | find /i /v "md5" | find /i /v "certutil" >> "%%f.checksum.txt"
    )
  )
)

echo Cleaning up...
REM Cleanup
if exist %GOBIN%\nvm-dev.exe del %GOBIN%\nvm-dev.exe

if exist "%DIST%" (
  echo NVM-DEV for Windows v%AppVersion% build completed.
  echo Distribution files are located in: %DIST%
) else (
  echo NVM-DEV for Windows v%AppVersion% build completed with warnings.
)
@echo on